package cn.edu.swu.syz.io;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ImageManager {
    private Map<String, String> imageMap = new HashMap<>();

    // 下载图片并将信息加入集合
    public void downloadImage(String url) {
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        String savePath = "images/" + fileName;
        FileUtils.ensureDirectoryExists("images");
        try {
            FileUtils.saveImage(url, savePath);
            imageMap.put(fileName, savePath);
            FileUtils.appendLog("Downloaded: " + url + " -> " + savePath);
            System.out.println("图片下载成功，保存路径：" + savePath);
        } catch (IOException e) {
            System.out.println("图片下载失败：" + e.getMessage());
        }
    }

    // 遍历集合，打印已下载图片列表
    public void listImages() {
        if (imageMap.isEmpty()) {
            System.out.println("暂无已下载的图片");
            return;
        }
        System.out.println("已下载图片列表：");
        for (Map.Entry<String, String> entry : imageMap.entrySet()) {
            System.out.println("图片名：" + entry.getKey() + "，路径：" + entry.getValue());
        }
    }

    // 根据名称查找图片路径
    public void searchImage(String name) {
        String path = imageMap.get(name);
        if (path != null) {
            System.out.println("找到图片，路径：" + path);
        } else {
            System.out.println("未找到名为 " + name + " 的图片");
        }
    }
}
